package com.fjnldx.mes.service.mes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.domain.pojo.ProductionPlan;
import com.fjnldx.mes.mapper.mes.MesMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class MesService {

    @Resource
    MesMapper mapper;


    /**
     * 新增生产计划-机台-mes_newscjh
     *
     * @param data
     * @return
     */
    public Result addProductionPlan(ProductionPlan data) {
        String msg = data.saveCheck();
        if (!StringUtils.isEmpty(msg)) {
            return Result.error(msg);
        }
        // 一天内不能重复插入相同机台
        QueryWrapper<ProductionPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("devSn", data.getDevSn());
        queryWrapper.apply(String.format("DATEDIFF(time,'%s')=0", data.getTime()));
        Integer i = mapper.selectCount(queryWrapper);
        if (i > 0) {
            return Result.error("一天内不能重复插入相同机台");
        }
        int insert = mapper.insert(data);
        return insert > 0 ? Result.success("新增成功") : Result.error("新增失败");
    }

    /**
     * 修改生产计划-机台-mes_newscjh
     *
     * @param data
     * @return
     */
    public Result modifyProductionPlan(ProductionPlan data) {
        String msg = data.updateCheck();
        if (!StringUtils.isEmpty(msg)) {
            return Result.error(msg);
        }
        int insert = mapper.updateById(data);
        return insert > 0 ? Result.success("更新成功") : Result.error("系统异常");
    }


    /**
     * 删除生产计划-机台-mes_newscjh
     *
     * @param array
     * @return
     */
    public Result deleteProductionPlan(String[] array) {
        if (null == array || array.length <= 0) {
            return Result.error("参数错误");
        }
        int i = mapper.deleteBatchIds(Arrays.stream(array).collect(Collectors.toList()));
        return i > 0 ? Result.success("删除成功") : Result.error("系统异常");
    }


    /**
     * 多条件查询生产计划-机台-mes_newscjh
     *
     * @param data
     * @return
     */
    public Result queryProductionPlan(ProductionPlan data, Page page) {
        QueryWrapper<ProductionPlan> queryWrapper = new QueryWrapper<>();
        if (data.getDevSn() != null && !data.getDevSn().isEmpty()) {
            queryWrapper.eq("devSn", data.getDevSn());
        }
        if (data.getPz() != null && !data.getPz().isEmpty()) {
            queryWrapper.eq("pz", data.getPz());
        }
        if (data.getZs() != null && !data.getZs().isEmpty()) {
            queryWrapper.eq("zs", data.getZs());
        }
        if (data.getGongxu() != null && !data.getGongxu().isEmpty()) {
            queryWrapper.eq("gongxu", data.getGongxu());
        }
        if (data.getDcg() != null && !data.getDcg().isEmpty()) {
            queryWrapper.eq("dcg", data.getDcg());
        }
        if (data.getGh() != null && !data.getGh().isEmpty()) {
            queryWrapper.eq("gh", data.getGh());
        }
        if (data.getTime() != null) {
            queryWrapper.apply(String.format("DATEDIFF(time,'%s')=0", data.getTime()));
        }
        if (data.getDs() != null && !data.getDs().isEmpty()) {
            queryWrapper.eq("ds", data.getDs());
        }
        if (data.getDl() != null && !data.getDl().isEmpty()) {
            queryWrapper.eq("dl", data.getDl());
        }
        if (data.getXiaolv() != null && !data.getXiaolv().isEmpty()) {
            queryWrapper.eq("xiaolv", data.getXiaolv());
        }
        if (data.getCs() != null && !data.getCs().isEmpty()) {
            queryWrapper.eq("cs", data.getCs());
        }
        if (data.getZl() != null && !data.getZl().isEmpty()) {
            queryWrapper.eq("zl", data.getZl());
        }
        if (data.getTc() != null && !data.getTc().isEmpty()) {
            queryWrapper.eq("tc", data.getTc());
        }
        if (data.getJisuan() != null && !data.getJisuan().isEmpty()) {
            queryWrapper.eq("jisuan", data.getDs());
        }
        Page<ProductionPlan> result = mapper.selectPage(page, queryWrapper);
        return Result.success(result);

    }
}
